﻿using gt.framework.Utils;
using gt.webapi.core.Extensions;
using System;
using System.ComponentModel;
using System.Web.Http;

namespace gt.webapi.core.sample.Controllers
{
    [RoutePrefix("api/test")]
    public class TestController : ApiControllerBase
    {
        [Route("ok1"), HttpGet]
        public IHttpActionResult Ok(int id)
        {
            return Success<int>(id);
        }

        [AllowAnonymous]
        [Route("ok2"), HttpGet]
        public IHttpActionResult Ok2(int id)
        {
            return Success<int>(id);
        }

        [Route("ok3"), HttpPost]
        public IHttpActionResult Ok3(User user)
        {
            return Success(string.Format("{0}:{1}", user.Id, user.Name));
        }

        [Route("error1"), HttpGet]
        public IHttpActionResult Error1(int id)
        {
            var errorCode = (int)ApiResponseCodeEnum.Param_Error;
            return Error(errorCode, EnumUtils.GetEnumItemDescription(typeof(ApiResponseCodeEnum), errorCode));
        }
        [Route("error2"), HttpGet]
        public IHttpActionResult Error2(int id)
        {
            throw new Exception("this is test error!");
        }
        [Route("error3"), HttpGet]
        public IHttpActionResult Error3(int id)
        {
            //no authority
            return Success<int>(id);
        }
    }

    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public enum ApiResponseCodeEnum : int
    {
        [Description("调用成功")]
        Success = 0,
        [Description("参数错误")]
        Param_Error = 1,
        [Description("登陆失败，账号或密码不正确！")]
        Login_AccountOrPasswordFailed = 5,
        [Description("获取Token失败")]
        Token_GetFailed = 10,
        [Description("无效Token")]
        Token_ValidFailed = 15,
        [Description("Token过期")]
        Token_Expired = 20,
        [Description("系统错误")]
        Unknow_Error = 999
    }
}
